<?php

	require_once 'includes/constants.php';

	class Mysql {
		private $conn;
		private $query;


		function __construct() {
			$this->conn = mysqli_connect("mysql6.000webhost.com", "a3333734_szlike", "sz28099578", "a3333734_c40G") or die('There was a problem connecting to the database.');
		}
		
		function verify_Username_and_Pass($un, $pwd) {
					
			$query = "SELECT *
					FROM membership
					WHERE username = ? AND password = ?
					LIMIT 1";
			
			if($stmt = $this->conn->prepare($query)) {
				$stmt->bind_param('ss', $un, $pwd);
				$stmt->execute();
				if($stmt->fetch()) {
					$stmt->close();
					return true;
				}
			}
			
		}
		
		function check_id($un) {
			$query = "SELECT *
					FROM membership
					WHERE username = ?
					LIMIT 1";
			
			if($stmt = $this->conn->prepare($query)) {
				$stmt->bind_param('s', $un);
				$stmt->execute();
				if($stmt->fetch()) {
					return true;
				}
			}
		}
		
		function check_Admin($un) {
			$query = "SELECT *
					FROM membership
					WHERE username = ? AND Type = \"Admin\"
					LIMIT 1";
			
			if($stmt = $this->conn->prepare($query)) {
				$stmt->bind_param('s', $un);
				$stmt->execute();
				if($stmt->fetch()) {
					return true;
				}
			}
		}
		
		function get_Profile($un) {
			$query = "SELECT Profile
					FROM membership
					WHERE username = ?";
			
			if($stmt = $this->conn->prepare($query)) {
				$stmt->bind_param('s', $un);
				$stmt->execute();
				$stmt->bind_result($result);
				$stmt->fetch();
				return $result;
			}
		}
		
		function get_Tutors() {
			$query = "SELECT username
					FROM membership
					WHERE type = \"Tutor\"";

			$stmt = $this->conn->query($query);
			
			while ($row = $stmt->fetch_array(MYSQLI_ASSOC)){
				echo $row['username'];
				echo "<br>";
			}
		}
		
		function get_Students() {
			$query = "SELECT username
					FROM membership
					WHERE type = \"Student\"";
			
			$stmt = $this->conn->query($query);
			
			while ($row = $stmt->fetch_array(MYSQLI_ASSOC)){
				echo $row['username'];
				echo "<br>";
			}
		}
		
		function modify_Profile($un, $profile) {
			$query = "UPDATE membership
					SET Profile = ?
					WHERE username = ?";
					
			if($stmt = $this->conn->prepare($query)) {
				$stmt->bind_param('ss', $profile, $un);
				$stmt->execute();
				$stmt->fetch();
			}
		}
		
		function modify_Type($un, $type) {
			$query = "UPDATE membership
					SET Type = ?
					WHERE username = ?";
					
			if($stmt = $this->conn->prepare($query)) {
				$stmt->bind_param('ss', $type, $un);
				$stmt->execute();
				$stmt->fetch();
			}
		}
		
		function register($un, $pwd) {
			$query = "SELECT *
					FROM membership
					WHERE username = ?
					LIMIT 1";
			
			if($stmt = $this->conn->prepare($query)) {
				$stmt->bind_param('s', $un);
				$stmt->execute();
				if(!$stmt->fetch()) {
					$stmt->close();
					
					$query = "INSERT INTO membership (username, password, Type)
					VALUES (?, ?, \"Student\")";
					if($stmt = $this->conn->prepare($query)) {
						$stmt->bind_param('ss', $un, $pwd);
						$stmt->execute();
					}
					return true;
				}
			}
			
		}
	}
?>